package leetcode;

public class MinimumSizeSubarraySum {

	public static void main(String[] args) {
		MinimumSizeSubarraySum object = new MinimumSizeSubarraySum();
		int[] nums = {2, 3, 1, 2, 4, 3};
		int s = 7;
		object.minSubArrayLen(s, nums);
	}
	
	public int minSubArrayLen(int s, int[] nums) {
		if (nums == null || nums.length <= 0) {
			return 0;
		}
		int left = 0;
		int right = 0;
		int sum = nums[0];
		int length = Integer.MAX_VALUE;
		while (right < nums.length) {
			if (sum >= s) {
				System.out.println(length);
				length = Math.min(length, right - left + 1);
				System.out.println("left" + left + " right " + right);
				sum -= nums[left++];
			} else if (sum < s) {
				right++;
				if(right == nums.length){
					break;
				}
				sum += nums[right];
			}
		}
		System.out.println("length: " + length);
		return length;
	}
}
